﻿
Namespace MVVM

    Public MustInherit Class EditFormViewModelBase
        Inherits ViewModelBase

#Region " Declarations "

        Private _objUIValidationErrorDictionary As New Dictionary(Of String, UIValidationError)
        Private _strErrorHeaderText As String = String.Empty
        Private _strErrorMessage As String = String.Empty
        Private _strWatermarkMessage As String = "Valid record"

#End Region

#Region " Properties "

        Public Property ErrorHeaderText() As String
            Get
                Return _strErrorHeaderText
            End Get
            Set(ByVal Value As String)
                _strErrorHeaderText = Value
                OnPropertyChanged("ErrorHeaderText")
            End Set
        End Property

        Public Property ErrorMessage() As String
            Get
                Return _strErrorMessage
            End Get
            Set(ByVal Value As String)
                _strErrorMessage = Value
                OnPropertyChanged("ErrorMessage")
            End Set
        End Property

        Public ReadOnly Property UIValidationErrorCount() As Integer
            Get
                Return _objUIValidationErrorDictionary.Count
            End Get
        End Property

        Public ReadOnly Property UIValidationErrorMessages() As String
            Get

                If UIValidationErrorCount > 0 Then

                    Dim sb As New System.Text.StringBuilder(1024)

                    For Each kvp As KeyValuePair(Of String, UIValidationError) In _objUIValidationErrorDictionary
                        sb.AppendLine(kvp.Value.ToFriendlyErrorMessage)
                    Next

                    Return sb.ToString

                Else
                    Return String.Empty
                End If

            End Get
        End Property

        Public Property WatermarkMessage() As String
            Get
                Return _strWatermarkMessage
            End Get
            Set(ByVal Value As String)
                _strWatermarkMessage = Value
                OnPropertyChanged("WatermarkMessage")
            End Set
        End Property

#End Region

#Region " Constructor "

        Public Sub New()
        End Sub

#End Region

#Region " UIValidationError Methods "

        Public Sub AddUIValidationError(ByVal e As UIValidationError)
            _objUIValidationErrorDictionary.Add(e.Key, e)
            OnPropertyChanged("UIValidationErrorMessages")
            OnPropertyChanged("UIValidationErrorCount")
        End Sub

        Public Sub RemoveUIValidationError(ByVal e As UIValidationError)
            _objUIValidationErrorDictionary.Remove(e.Key)
            OnPropertyChanged("UIValidationErrorMessages")
            OnPropertyChanged("UIValidationErrorCount")
        End Sub

        Protected Sub ClearUIValidationErrors()
            _objUIValidationErrorDictionary.Clear()
            OnPropertyChanged("UIValidationErrorMessages")
            OnPropertyChanged("UIValidationErrorCount")
        End Sub

#End Region

    End Class

End Namespace
